// ConstSpringView.cpp : implementation file
//

#include "stdafx.h"
#include "PartConstSpringView.h"
#include "phsfactory.h"
#include "resourcedefine.h"

#ifdef _DEBUG
#define new DEBUG_NEW
#undef THIS_FILE
static char THIS_FILE[] = __FILE__;
#endif

/////////////////////////////////////////////////////////////////////////////
// CPartConstSpringView

IMPLEMENT_DYNCREATE(CPartConstSpringView, CFormViewEx)

CPartConstSpringView::CPartConstSpringView()
	: CFormViewEx(CPartConstSpringView::IDD)
{
	INIT_RESOURCE
	//{{AFX_DATA_INIT(CPartConstSpringView)
	m_strAssemblyLoad = _T("");
	m_strCustomID = _T("");
	m_strDim = _T("");
	m_strHotDisplacement = _T("");
	m_strName = _T("");
	m_strSpringNo = _T("");
	m_strSumDisplacement = _T("");
	m_strWeight = _T("");
	m_strWorkLoad = _T("");
	m_strDisplacement = _T("");
	m_strNutDia = _T("");
	m_strSpec = _T("");
	m_strClgg = _T("");
	//}}AFX_DATA_INIT
	m_pObject = NULL;
	m_bIsFirstVisible = TRUE;
}

CPartConstSpringView::~CPartConstSpringView()
{
}

void CPartConstSpringView::DoDataExchange(CDataExchange* pDX)
{
	CFormView::DoDataExchange(pDX);
	//{{AFX_DATA_MAP(CPartConstSpringView)
	DDX_Text(pDX, IDC_ASSEMBLYLOAD_EDIT, m_strAssemblyLoad);
	DDX_Text(pDX, IDC_CUSTOMID_EDIT, m_strCustomID);
	DDX_Text(pDX, IDC_DIM_EDIT, m_strDim);
	DDX_Text(pDX, IDC_HOTDISPLACEMENT_EDIT, m_strHotDisplacement);
	DDX_Text(pDX, IDC_NAME_EDIT, m_strName);
	DDX_CBString(pDX, IDC_SPRINGNO_COMBO, m_strSpringNo);
	DDX_Text(pDX, IDC_SUMDISPLACEMENT_EDIT, m_strSumDisplacement);
	DDX_Text(pDX, IDC_WEIGHT_EDIT, m_strWeight);
	DDX_Text(pDX, IDC_WORKLOAD_EDIT, m_strWorkLoad);
	DDX_CBString(pDX, IDC_DISPLACEMENT_COMBO, m_strDisplacement);
	DDX_CBString(pDX, IDC_NUTDIA_COMBO, m_strNutDia);
	DDX_CBString(pDX, IDC_SPEC_COMBO, m_strSpec);
	DDX_Text(pDX, IDC_CLGG_EDIT, m_strClgg);
	//}}AFX_DATA_MAP
}


BEGIN_MESSAGE_MAP(CPartConstSpringView, CFormViewEx)
	//{{AFX_MSG_MAP(CPartConstSpringView)
	ON_WM_CREATE()
	ON_WM_DESTROY()
	ON_WM_SHOWWINDOW()
	ON_EN_CHANGE(IDC_ASSEMBLYLOAD_EDIT, OnChangeAssemblyloadEdit)
	ON_CBN_EDITCHANGE(IDC_SPEC_COMBO, OnEditchangeSpecCombo)
	ON_CBN_SELCHANGE(IDC_SPEC_COMBO, OnSelchangeSpecCombo)
	//}}AFX_MSG_MAP
END_MESSAGE_MAP()

/////////////////////////////////////////////////////////////////////////////
// CConstSpringView diagnostics

#ifdef _DEBUG
void CPartConstSpringView::AssertValid() const
{
	CFormView::AssertValid();
}

void CPartConstSpringView::Dump(CDumpContext& dc) const
{
	CFormView::Dump(dc);
}
#endif //_DEBUG

/////////////////////////////////////////////////////////////////////////////
// CConstSpringView message handlers

int CPartConstSpringView::OnCreate(LPCREATESTRUCT lpCreateStruct) 
{
	if (CFormView::OnCreate(lpCreateStruct) == -1)
		return -1;
	
	END_RESOURCE
	
	m_strAssemblyLoad.Format(_T("%g"), m_pObject->GetAssemblyLoad());
	m_strCustomID = m_pObject->GetCustomID();
	m_strDim.Format(_T("%g"), m_pObject->GetDim());
	m_strHotDisplacement.Format(_T("%g"), m_pObject->GetHeatDisplacement());
	m_strName = m_pObject->GetName();
	m_strNutDia.Format(_T("%g"),m_pObject->GetNutDia());
	m_strSpec = m_pObject->GetBH();
	m_strSpringNo = m_pObject->GetSpringNo();
	m_strTable = m_pObject->GetTableName();
//	m_strTemperature.Format(_T("%g"), m_pObject->GetTemperature());
	m_strWeight.Format(_T("%g"), m_pObject->GetWeight());
	m_strWorkLoad.Format(_T("%g"), m_pObject->GetWorkLoad());
	m_strClgg = m_pObject->GetSpec();
	
	return 0;
}

void CPartConstSpringView::OnDestroy() 
{
	CFormView::OnDestroy();
	
	UpDateObjectData();
}

void CPartConstSpringView::OnShowWindow(BOOL bShow, UINT nStatus) 
{
	CFormView::OnShowWindow(bShow, nStatus);
	if(m_bIsFirstVisible)
	{
		LoadBH();
		LoadNutDia();
		LoadOthers();
		UpdateData(FALSE);
		m_bIsFirstVisible = FALSE;
	}
/*	if(!bShow)
	{
		UpDateObjectData();
	}*/
}

void CPartConstSpringView::LoadBH()
{
	CStringArray* pArr = NULL;
	INT nCount = 0;

	CComboBox* pSpecCombo = static_cast<CComboBox*>(GetDlgItem(IDC_SPEC_COMBO));
	if(pSpecCombo == NULL)
	{
		return;
	}
	pSpecCombo->ResetContent();
	
	pArr = GetCurDatabase().LoadConstSpringBH(this->m_pObject->GetStandard(),this->m_strTable, m_pObject->GetNutTableName(),
		this->m_strCustomID, atof(this->m_strAssemblyLoad));
	if(pArr == NULL)
	{
		return;
	}
	for(int i = 0; i < pArr->GetSize(); i++)
	{
		int j = pSpecCombo->AddString(pArr->GetAt(i));
		if(m_strSpec == pArr->GetAt(i))
		{
			nCount = j;
		}
	}
	delete pArr;
	
	if(CB_ERR == pSpecCombo->SetCurSel(nCount))
		return;
	pSpecCombo->GetWindowText(m_strSpec);

	LoadDH();
	this->LoadSpringDisplacement();
}

void CPartConstSpringView::LoadDH()
{
	CStringArray* pArr = NULL;
	INT nCount = 0;

	CComboBox* pCombo = static_cast<CComboBox*>(GetDlgItem(IDC_SPRINGNO_COMBO));
	if(pCombo == NULL)
	{
		return;
	}
	pCombo->ResetContent();
	
	pArr = GetCurDatabase().LoadDH(this->m_pObject->GetStandard(),this->m_strTable, this->m_strCustomID, this->m_strSpec);
	if(pArr == NULL)
	{
		return;
	}
	for(int i = 0; i < pArr->GetSize(); i++)
	{
		int j = pCombo->AddString(pArr->GetAt(i));
		if(m_strSpringNo == pArr->GetAt(i))
			nCount = j;
	}
	delete pArr;
	
	if(CB_ERR == pCombo->SetCurSel(nCount))
		return;
	pCombo->GetWindowText(m_strSpringNo);

}

void CPartConstSpringView::LoadOthers()
{
	PartConstSpring part;
	part.SetBH(this->m_strSpec);
	part.SetCustomID(this->m_strCustomID);
	part.SetClassID(m_pObject->GetClassID());
	part.SetIndex(m_pObject->GetIndex());
	part.SetTableName(m_pObject->GetTableName());
	part.SetStandard(this->m_pObject->GetStandard());

	GetCurDatabase().LoadOthers(part);

	m_strDim.Format(_T("%g"), part.GetDim());
	m_strWeight.Format(_T("%g"), part.GetUnitWeight());

	UpdateData(FALSE);
}

void CPartConstSpringView::LoadNutDia()
{
	CStringArray* pArr = NULL;
	INT nCount = 0;

	CComboBox* pCombo = static_cast<CComboBox*>(GetDlgItem(IDC_NUTDIA_COMBO));
	if(pCombo == NULL)
	{
		return;
	}
	pCombo->ResetContent();
	
	pArr = GetCurDatabase().LoadConstSpringNutDia(this->m_pObject->GetStandard(),m_pObject->GetNutTableName(),
		this->m_strCustomID, atof(this->m_strAssemblyLoad));
	if(pArr == NULL)
	{
		return;
	}
	for(int i = 0; i < pArr->GetSize(); i++)
	{
		int j = pCombo->AddString(pArr->GetAt(i));
		if(m_strNutDia == pArr->GetAt(i))
		{
			nCount = j;
		}
	}
	delete pArr;

	if(CB_ERR == pCombo->SetCurSel(nCount))
		return;
	pCombo->GetWindowText(m_strNutDia);
}

void CPartConstSpringView::UpDateObjectData()
{
	UpdateData();

	if(m_strSpec != m_pObject->GetBH())
	{
		m_pObject->SetBH(m_strSpec);
		GetCurDatabase().LoadOthers(*m_pObject);
	}

	m_pObject->SetAssemblyLoad(atof(m_strAssemblyLoad));
	m_pObject->SetDim(atof(m_strDim));
	m_pObject->SetHeatDisplacement(atof(m_strHotDisplacement));
	m_pObject->SetNutDia(atof(m_strNutDia));
//	m_pObject->SetRecduce(atof(m_strReduce));
	m_pObject->SetSpringNo(m_strSpringNo);
//	m_pObject->SetTemperature(atof(m_strTemperature));
	m_pObject->SetUnitWeight(atof(m_strWeight));
	m_pObject->SetWorkLoad(atof(m_strWorkLoad));
}


void CPartConstSpringView::LoadSpringDisplacement()
{
	CStringArray* pArr = NULL;
	INT nCount = 0;

	CComboBox* pCombo = static_cast<CComboBox*>(GetDlgItem(IDC_DISPLACEMENT_COMBO));
	if(pCombo == NULL)
	{
		return;
	}
	pCombo->ResetContent();
	
	pArr = GetCurDatabase().LoadSpringDisplacement(this->m_pObject->GetStandard(),this->m_strTable,
		this->m_strCustomID, this->m_strSpec, this->m_theMin, this->m_theMax);
	if(pArr == NULL)
	{
		return;
	}
	for(int i = 0; i < pArr->GetSize(); i++)
	{
		int j = pCombo->AddString(pArr->GetAt(i));
		if(this->m_strDisplacement == pArr->GetAt(i))
		{
			nCount = j;
		}
	}
	delete pArr;

	if(CB_ERR == pCombo->SetCurSel(nCount))
		return;
	pCombo->GetWindowText(m_strDisplacement);
}

void CPartConstSpringView::OnChangeAssemblyloadEdit() 
{
	UpdateData();
	
	LoadBH();
	this->LoadNutDia();
	this->LoadOthers();
	this->LoadSpec();
}

void CPartConstSpringView::OnEditchangeSpecCombo() 
{
}

void CPartConstSpringView::OnSelchangeSpecCombo() 
{
	UpdateData();
	this->LoadDH();
	this->LoadSpringDisplacement();
	this->LoadOthers();
	this->LoadSpec();
}

void CPartConstSpringView::LoadSpec()
{
	this->UpDateObjectData();
	this->m_strClgg = m_pObject->GetSpec();
	this->UpdateData(FALSE);
}